import {Component, Input, OnInit} from '@angular/core';
import {ModalController, NavController} from "@ionic/angular";
import {ActivatedRoute, Router} from "@angular/router";
import {HttpService} from "../../services/http/http.service";
import {NativeService} from "../../services/native.service";
import {API} from "../../services/API";
import {ModifyInformationComponent} from "../modify-information/modify-information.component";

@Component({
  selector: 'app-select-community',
  templateUrl: './select-community.component.html',
  styleUrls: ['./select-community.component.scss'],
})
export class SelectCommunityComponent implements OnInit {
  addressList;
  @Input() code;
  @Input() option;  // 市
  @Input() flag;
  name;
  @Input() summaryId;
  @Input() serialNo;
  @Input() province; // 省
  @Input() resiCity; // 市
  @Input() restDistrict; // 区
  @Input() inputTime; // 插入时间

  constructor(
      private nav: NavController,
      private router: Router,
      private activatedRoute: ActivatedRoute,
      private httpService: HttpService,
      private nativeService: NativeService,
      private modalCtrl : ModalController
  ) {
    this.activatedRoute.queryParams.subscribe(params=>{
      console.log('选择小区接受传参值：')
      console.log(params);
      this.code = params.code;
      this.flag = params.flag;
      this.option = params.option;
      this.summaryId = params.summaryId;
      this.serialNo = params.serialNo;
      this.province = params.province;
      this.resiCity = params.resiCity;
      this.restDistrict = params.restDistrict;
      this.inputTime = params.inputTime;
      
    })
  }

  ngOnInit() {
  }

  async select(){
    if (this.code) {
      const param = {
        cityCode: this.code,
        name: this.name,
      }
      const info = await this.httpService.post(API.COMMUNITY_SEARCH, param, false);
      console.log(info);
      console.log(info);
      const response = Object.values(info);
      console.log(response);
      // @ts-ignore
      this.addressList = response[0].data;
    } else {
      this.nativeService.showAlert('请先选择地区', '确定')
    }
  }

  async selectAddress(item: any) {
    this.modalCtrl.dismiss();
    console.log(item);
    let select = await this.modalCtrl.create({
      component: ModifyInformationComponent,
      cssClass: 'toast',
      showBackdrop: true,
      backdropDismiss: true,
      componentProps: {
        address: item.address,
        name: item.name,
        city:  this.option,
        communityId:  item.communityId,
        summaryId: this.summaryId,
        code: this.code,
        serialNo: this.serialNo,
        province: this.province,
        resiCity: this.resiCity,
        restDistrict: this.restDistrict,
        flag: this.flag,
      }
    });
    select.present();
  }
  back() {
      this.modalCtrl.dismiss();
  }
}
